package com.demandbreakdown.xiyun.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.demandbreakdown.xiyun.common.annotation.ApiLog;
import com.demandbreakdown.xiyun.common.base.Result;
import com.demandbreakdown.xiyun.common.base.ResultUtils;
import com.demandbreakdown.xiyun.service.StorageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import static com.demandbreakdown.xiyun.common.constant.PictureConstant.ASSET_TYPE_AVATAR;
import static com.demandbreakdown.xiyun.common.constant.PictureConstant.ASSET_TYPE_COVER;

/**
 * 资源上传控制器
 * 用于头像、封面等辅助资源的简单上传（不保存数据库记录）
 *
 * @author poxz
 */
@RestController
@RequestMapping("/upload")
@RequiredArgsConstructor
@Tag(name = "资源上传", description = "头像、空间封面等辅助资源上传接口")
public class AssetUploadController {

    private final StorageService storageService;

    @ApiLog("上传用户头像")
    @Operation(summary = "上传用户头像", description = "上传用户头像，支持2MB以内的图片格式，返回存储URL")
    @PostMapping("/avatar")
    public Result<String> uploadAvatar(
            @Parameter(description = "头像图片文件，支持常见图片格式，大小不超过2MB", required = true)
            @RequestParam("file") MultipartFile file) {
        Long userId = StpUtil.getLoginIdAsLong();
        String fileUrl = storageService.uploadAsset(file, ASSET_TYPE_AVATAR, userId);
        return ResultUtils.success(fileUrl);
    }

    @ApiLog("上传空间封面")
    @Operation(summary = "上传空间封面", description = "上传空间封面图片，支持5MB以内的图片格式，返回存储URL")
    @PostMapping("/cover")
    public Result<String> uploadCover(
            @Parameter(description = "封面图片文件，支持常见图片格式，大小不超过5MB", required = true)
            @RequestParam("file") MultipartFile file) {
        Long userId = StpUtil.getLoginIdAsLong();
        String fileUrl = storageService.uploadAsset(file, ASSET_TYPE_COVER, userId);
        return ResultUtils.success(fileUrl);
    }
}

